﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Web;
using BLL;
using DAL;
using Ext.Net;
using Roles;
namespace ERP.Page
{
    public partial class Index : System.Web.UI.Page
    {
        #region[Define]
        private DataTable _dtGetUserInfo;
        private DataTable _dtGetUser;
        private readonly BllUser _clsUser = new BllUser();
        private readonly string pathFolderRoot = ConfigurationManager.AppSettings["FolderRoot"];
        #endregion

        #region[Controler]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["login"] == null && Session["UserName"] == null)
            {
                Response.Redirect(@"../Login.aspx");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
                Response.Cache.SetNoStore();
            }
            else
            {
                if ((bool)Session["login"] == false)
                {
                    Response.Redirect(@"../Login.aspx");
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
                    Response.Cache.SetNoStore();
                }
                else
                {
                    //CheckPermisstion();
                    lbServerNameTitile.Text = Session["UserName"].ToString();
                    //Get MachineName
                    lbMachineName.Text = Environment.MachineName;
                    //Get Ip Address
                    var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (var networkInterface in networkInterfaces)
                    {
                        lbIdAdress.Text =
                            GetLocalIPv4(networkInterface.NetworkInterfaceType != NetworkInterfaceType.Wireless80211
                                ? NetworkInterfaceType.Ethernet
                                : NetworkInterfaceType.Wireless80211);
                    }
                    //Get Session Status
                    var o1 = Session["login"];
                    if (o1 != null) lbSessionStatus.Text = o1.ToString();
                    if (!X.IsAjaxRequest)
                    {
                        GetFolderRoot();
                    }
                }
            }
            if (X.IsAjaxRequest)
            {
                ResourceManager.StartupMask = null;
            }
        }
        //Menu-Click
        protected void MenuItemExit_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/");
                var obj = new EtUser {UsersId = int.Parse(Session["Users_Id"].ToString())};
                BllUser.UpdateLastLogin(obj);
                if (File.Exists(path + "Session.xml"))
                    File.Delete(path + "Session.xml");
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
            Session["login"] = false;
            Session.RemoveAll();
            Session.Abandon();
            Response.Redirect(@"../Login.aspx");
        }
        protected void MenuItemChangePage_Click(object sender, DirectEventArgs e)
        {
            txtPassOld.Text = string.Empty;
            txtNewPass.Text = string.Empty;
            txtConfirmPasswords.Text = string.Empty;
            WindowChangePass.Show();
            txtPassOld.Focus(false, 100);
        }
        protected void MenuItemUserInfo_Click(object sender, DirectEventArgs e)
        {
            GetUserInfo();
            WindowUserInfo.Show();
        }
        protected void MenuItemAbout_Click(object sender, DirectEventArgs e)
        {
            WindowSoftInfo.Show();
            GetLicense();
        }
        //Button
        protected void btnChangePass_Click(object sender, DirectEventArgs e)
        {
            if (String.IsNullOrEmpty(txtPassOld.Text) && String.IsNullOrEmpty(txtNewPass.Text) &&
                String.IsNullOrEmpty(txtConfirmPasswords.Text))
            {
                HTML.clsFormat.Alert_ERROR("Thông Báo", "Vui lòng điền vào đầy đủ thông tin để thay đổi mật khẩu !");
            }
            else
            {
                ChangePass();
            }
        }
        protected void btnCancel_Click(object sender, DirectEventArgs e)
        {
            WindowChangePass.Close();
        }
        protected void btnClose_Click(object sender, DirectEventArgs e)
        {
            WindowUserInfo.Close();
        }
        protected void btnCloseSoftInfo_Click(object sender, DirectEventArgs e)
        {
            WindowSoftInfo.Close();
        }
        #endregion

        #region[Method]
        private void GetUserInfo()
        {
            _dtGetUserInfo = _clsUser.GetUserEdit(int.Parse(Session["Users_Id"].ToString()));
            if (_dtGetUserInfo != null && _dtGetUserInfo.Rows.Count > 0)
            {
                lbUserName.Text = _dtGetUserInfo.Rows[0]["UserName"].ToString();
                lbFullName.Text = _dtGetUserInfo.Rows[0]["FullName"].ToString();
                lbJob.Text = _dtGetUserInfo.Rows[0]["Job_Name"].ToString();
                lbRoles.Text = _dtGetUserInfo.Rows[0]["Group_Name"].ToString();
                lbCellPhone.Text = _dtGetUserInfo.Rows[0]["CellPhone"].ToString();
                lbEmail.Text = _dtGetUserInfo.Rows[0]["Email"].ToString();
                lbCreateDate.Text = String.Format("{0:dd/MM/yyyy}", _dtGetUserInfo.Rows[0]["CreatedDate"]);
            }
        }
        private void UpdatePass()
        {
            try
            {
                var obj = new EtUser
                {
                    UsersId = int.Parse(Session["Users_Id"].ToString()),
                    Passwords = Encode.Encode.GetMD5(txtConfirmPasswords.Text).ToUpper()
                };
                BllUser.ChangePass(obj);
                HTML.clsFormat.Alert_INFO("Thông Báo", "Thay đổi mật khẩu thành công !");
                WindowChangePass.Close();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void ChangePass()
        {
            string oldpass = Encode.Encode.GetMD5(txtPassOld.Text);
            _dtGetUser = _clsUser.GetUserEdit(int.Parse(Session["Users_Id"].ToString()));
            if (_dtGetUser != null && _dtGetUser.Rows.Count > 0)
            {
                if (_dtGetUser.Rows[0]["Passwords"].ToString() != oldpass.ToUpper())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Mật khẩu cũ không chính xác !");
                }
                else
                {
                    UpdatePass();
                }
            }
        }
        #endregion

        //#region[Permisstion]
        //private void CheckPermisstion()
        //{
        //    if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
        //    {
        //        #region[Modules-Main]
        //        var Is_PageQuanTriHeThong = SecurityServices.HasPermisstionPageMain(StringContent.QuanTriHeThong, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageQuanTriHeThong == false)
        //            {
        //                PanelQuanTriHeThong.Visible = false;
        //            }
        //        }
        //        var Is_PageMuaHang = SecurityServices.HasPermisstionPageMain(StringContent.MuaHang, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageMuaHang == false)
        //            {
        //                PanelMuaHang.Visible = false;
        //            }
        //        }
        //        var Is_PageBanHang = SecurityServices.HasPermisstionPageMain(StringContent.BanHang, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageBanHang == false)
        //            {
        //                PanelBanHang.Visible = false;
        //            }
        //        }
        //        var Is_PageCongTy = SecurityServices.HasPermisstionPageMain(StringContent.CongTy, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageCongTy == false)
        //            {
        //                PanelCongTy.Visible = false;
        //            }
        //        }
        //        var Is_PageTuDienKho = SecurityServices.HasPermisstionPageMain(StringContent.TuDienKho, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageTuDienKho == false)
        //            {
        //                PanelTuDienKho.Visible = false;
        //            }
        //        }
        //        var Is_PageQuanLyKho = SecurityServices.HasPermisstionPageMain(StringContent.QuanLyKho, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageQuanLyKho == false)
        //            {
        //                PanelQuanLyKhoKho.Visible = false;
        //            }
        //        }
        //        var Is_PageDanhMucChung = SecurityServices.HasPermisstionPageMain(StringContent.DanhMucChung, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageDanhMucChung == false)
        //            {
        //                PanelDanhMucChung.Visible = false;
        //            }
        //        }
        //        var Is_PageKeToan = SecurityServices.HasPermisstionPageMain(StringContent.KeToan, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageKeToan == false)
        //            {
        //                PanelKeToanModule.Visible = false;
        //            }
        //        }
        //        var Is_PageKhachHang = SecurityServices.HasPermisstionPageMain(StringContent.KhachHang, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageKhachHang == false)
        //            {
        //                PanelKhachHang.Visible = false;
        //            }
        //        }
        //        var Is_PageNhanVien = SecurityServices.HasPermisstionPageMain(StringContent.NhanVien, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageNhanVien == false)
        //            {
        //                PanelNhanVien.Visible = false;
        //            }
        //        }
        //        var Is_PageBaoCaoThongKe = SecurityServices.HasPermisstionPageMain(StringContent.BaoCaoThongKe, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageBaoCaoThongKe == false)
        //            {
        //                PanelThongKeVaBaoCao.Visible = false;
        //            }
        //        }
        //        var Is_PageQuanLyPhieuThu = SecurityServices.HasPermisstionPageMain(StringContent.QuanLyPhieuThu, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageQuanLyPhieuThu == false)
        //            {
        //                PanelQuanLyPhieuThu.Visible = false;
        //            }
        //        }
        //        var Is_PageQuanLyPhieuChi = SecurityServices.HasPermisstionPageMain(StringContent.QuanLyPhieuChi, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageQuanLyPhieuChi == false)
        //            {
        //                PanelQuanLyPhieuChi.Visible = false;
        //            }
        //        }
        //        var Is_PageQuanLyNganQuy = SecurityServices.HasPermisstionPageMain(StringContent.QuanLyNganQuy, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageQuanLyNganQuy == false)
        //            {
        //                PanelQuanLyNganQuy.Visible = false;
        //            }
        //        }
        //        var Is_PageDichVu = SecurityServices.HasPermisstionPageMain(StringContent.QuanLyDichVu, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageDichVu == false)
        //            {
        //                PanelDichVu.Visible = false;
        //            }
        //        }
        //        #endregion

        //        #region[Modules-Parrent]
        //        var Is_PageNhomQuyen = SecurityServices.HasPermisstionPageParrent(StringContent.NhomQuyen, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageNhomQuyen == false)
        //            {
        //                linkNhomQuyen.Visible = false;
        //            }
        //        }
        //        var Is_PageModulesMain = SecurityServices.HasPermisstionPageParrent(StringContent.ModulesMain, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageModulesMain == false)
        //            {
        //                linkModulesMain.Visible = false;
        //            }
        //        }
        //        var Is_PageModulesParent = SecurityServices.HasPermisstionPageParrent(StringContent.ModulesParent, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageModulesParent == false)
        //            {
        //                linkModulesParrent.Visible = false;
        //            }
        //        }
        //        var Is_PageUserPermisstion = SecurityServices.HasPermisstionPageParrent(StringContent.UserPermisstion, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageUserPermisstion == false)
        //            {
        //                linkRoles.Visible = false;
        //            }
        //        }
        //        var Is_PageUserManager = SecurityServices.HasPermisstionPageParrent(StringContent.UserManager, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageUserManager == false)
        //            {
        //                linkUser.Visible = false;
        //            }
        //        }
        //        var Is_PageFileManager= SecurityServices.HasPermisstionPageParrent(StringContent.FileManager, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageFileManager == false)
        //            {
        //                linkFileManager.Visible = false;
        //            }
        //        }
        //        var Is_PageOrder = SecurityServices.HasPermisstionPageParrent(StringContent.Order, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageOrder == false)
        //            {
        //                ShowTabDonDatHang.Visible = false;
        //            }
        //        }
        //        var Is_PageOrderBuyMain = SecurityServices.HasPermisstionPageParrent(StringContent.OrderBuyMain, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageOrderBuyMain == false)
        //            {
        //                ShowTabMuaHang.Visible = false;
        //            }
        //        }
        //        var Is_PageOrderSell = SecurityServices.HasPermisstionPageParrent(StringContent.OrderBuySell, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageOrderSell == false)
        //            {
        //                ShowTabDonBanHang.Visible = false;
        //            }
        //        }
        //        var Is_PageDepartment = SecurityServices.HasPermisstionPageParrent(StringContent.Department, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageDepartment == false)
        //            {
        //                ShowTabCongTy.Visible = false;
        //            }
        //        }
        //        var Is_PagePart = SecurityServices.HasPermisstionPageParrent(StringContent.Part, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PagePart == false)
        //            {
        //                ShowTabBoPhan.Visible = false;
        //            }
        //        }
        //        var Is_PageJob = SecurityServices.HasPermisstionPageParrent(StringContent.Job, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageJob == false)
        //            {
        //                ShowTabChucVu.Visible = false;
        //            }
        //        }
        //        var Is_PageBranch = SecurityServices.HasPermisstionPageParrent(StringContent.Branch, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageBranch == false)
        //            {
        //                ShowTabChiNhanh.Visible = false;
        //            }
        //        }
        //        var Is_PageRoom = SecurityServices.HasPermisstionPageParrent(StringContent.Room, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageRoom == false)
        //            {
        //                ShowTabPhongChua.Visible = false;
        //            }
        //        }
        //        var Is_PageWarehouse = SecurityServices.HasPermisstionPageParrent(StringContent.Warehouse, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageWarehouse == false)
        //            {
        //                ShowTabKho.Visible = false;
        //            }
        //        }
        //        var Is_PageManufacturer = SecurityServices.HasPermisstionPageParrent(StringContent.Manufacturer, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageManufacturer == false)
        //            {
        //                ShowTabManufacturer.Visible = false;
        //            }
        //        }
        //        var Is_PageUnit = SecurityServices.HasPermisstionPageParrent(StringContent.Unit, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageUnit == false)
        //            {
        //                ShowTabDVT.Visible = false;
        //            }
        //        }
        //        var Is_PageMaterials_Type = SecurityServices.HasPermisstionPageParrent(StringContent.Materials_Type, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageMaterials_Type == false)
        //            {
        //                ShowTabNhomVatTu.Visible = false;
        //            }
        //        }
        //        var Is_PageVatTu = SecurityServices.HasPermisstionPageParrent(StringContent.VatTu, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageVatTu == false)
        //            {
        //                ShowTabVatTu.Visible = false;
        //            }
        //        }
        //        var Is_PageEnterprise = SecurityServices.HasPermisstionPageParrent(StringContent.Enterprise, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageEnterprise == false)
        //            {
        //                ShowTabLoaiDoanhNghiep.Visible = false;
        //            }
        //        }
        //        var Is_PageBank = SecurityServices.HasPermisstionPageParrent(StringContent.Bank, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageBank == false)
        //            {
        //                ShowTabNganHang.Visible = false;
        //            }
        //        }
        //        var Is_PageEthnic = SecurityServices.HasPermisstionPageParrent(StringContent.Ethnic, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageEthnic == false)
        //            {
        //                ShowTabDanToc.Visible = false;
        //            }
        //        }
        //        var Is_PageCurrency = SecurityServices.HasPermisstionPageParrent(StringContent.Currency, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageCurrency == false)
        //            {
        //                ShowTabTienTe.Visible = false;
        //            }
        //        }
        //        var Is_PageNational = SecurityServices.HasPermisstionPageParrent(StringContent.National, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageNational == false)
        //            {
        //                ShowTabQuocGia.Visible = false;
        //            }
        //        }
        //        var Is_PageDomain = SecurityServices.HasPermisstionPageParrent(StringContent.Domain, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageDomain == false)
        //            {
        //                ShowTabMien.Visible = false;
        //            }
        //        }
        //        var Is_PageProvince = SecurityServices.HasPermisstionPageParrent(StringContent.Province, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageProvince == false)
        //            {
        //                ShowTabTinh.Visible = false;
        //            }
        //        }
        //        var Is_PageDistricts = SecurityServices.HasPermisstionPageParrent(StringContent.Districts, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageDistricts == false)
        //            {
        //                ShowTabQuanHuyen.Visible = false;
        //            }
        //        }
        //        var Is_PageIncome_Expenditure_Method = SecurityServices.HasPermisstionPageParrent(StringContent.Income_Expenditure_Method, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageIncome_Expenditure_Method == false)
        //            {
        //                ShowTabKhoanMucThuChi.Visible = false;
        //            }
        //        }
        //        var Is_PageBank_Account = SecurityServices.HasPermisstionPageParrent(StringContent.Bank_Account, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageBank_Account == false)
        //            {
        //                ShowTabTaiKhoanThuChi.Visible = false;
        //            }
        //        }
        //        var Is_PagePhanHeKeToan= SecurityServices.HasPermisstionPageParrent(StringContent.AssignedAccountLeve, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PagePhanHeKeToan == false)
        //            {
        //                ShowTabPhanHeKeToan1.Visible = false;
        //            }
        //        }
        //        var Is_PagePhanHeKeToan2 = SecurityServices.HasPermisstionPageParrent(StringContent.AssignedAccountLeve2, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PagePhanHeKeToan2 == false)
        //            {
        //                ShowTabPhanHeKeToan2.Visible = false;
        //            }
        //        }
        //        var Is_PageVocative = SecurityServices.HasPermisstionPageParrent(StringContent.Vocative, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageVocative == false)
        //            {
        //                ShowTabDanhXung.Visible = false;
        //            }
        //        }
        //        var Is_PageCustomer = SecurityServices.HasPermisstionPageParrent(StringContent.Customer, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageCustomer == false)
        //            {
        //                ShowTabKhachHang.Visible = false;
        //            }
        //        }
        //        var Is_PageCustomer_Type = SecurityServices.HasPermisstionPageParrent(StringContent.Customer_Type, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageCustomer_Type == false)
        //            {
        //                ShowTabLoaiKhachHang.Visible = false;
        //            }
        //        }
        //        var Is_PageDestination = SecurityServices.HasPermisstionPageParrent(StringContent.Destination, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageDestination == false)
        //            {
        //                ShowTabMucDich.Visible = false;
        //            }
        //        }
        //        var Is_PageContact = SecurityServices.HasPermisstionPageParrent(StringContent.Contact, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageContact == false)
        //            {
        //                ShowTabNguoiLienHe.Visible = false;
        //            }
        //        }
        //        var Is_PageSector = SecurityServices.HasPermisstionPageParrent(StringContent.Sector, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageSector == false)
        //            {
        //                ShowTabNganhNghe.Visible = false;
        //            }
        //        }
        //        var Is_PageMethod = SecurityServices.HasPermisstionPageParrent(StringContent.Method, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageMethod == false)
        //            {
        //                ShowTabPhuongThuc.Visible = false;
        //            }
        //        }
        //        var Is_PageTransaction = SecurityServices.HasPermisstionPageParrent(StringContent.Transaction, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageTransaction == false)
        //            {
        //                ShowTabGiaoDich.Visible = false;
        //            }
        //        }
        //        var Is_PageQuotes = SecurityServices.HasPermisstionPageParrent(StringContent.Quotes, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageQuotes == false)
        //            {
        //                ShowTabBaoGia.Visible = false;
        //            }
        //        }
        //        var Is_PageEmployee = SecurityServices.HasPermisstionPageParrent(StringContent.Employee, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageEmployee == false)
        //            {
        //                linkNhanVien.Visible = false;
        //            }
        //        }
        //        var Is_PageStatistics = SecurityServices.HasPermisstionPageParrent(StringContent.Statistics, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageStatistics == false)
        //            {
        //                ShowTabThongKeDonHang.Visible = false;
        //            }
        //        }
        //        var Is_PageProposed = SecurityServices.HasPermisstionPageParrent(StringContent.Proposed, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageProposed == false)
        //            {
        //                ShowTabDeNghiNhapXuat.Visible = false;
        //            }
        //        }
        //        var Is_PageImport = SecurityServices.HasPermisstionPageParrent(StringContent.Import, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageImport == false)
        //            {
        //                ShowTabNhapKho.Visible = false;
        //            }
        //        }
        //        var Is_PageServicesMaintain = SecurityServices.HasPermisstionPageParrent(StringContent.ServicesMaintain, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageServicesMaintain == false)
        //            {
        //                ShowTabBaoHanh.Visible = false;
        //            }
        //        }
        //        var Is_PageServicesMainMailSetting = SecurityServices.HasPermisstionPageParrent(StringContent.MailSetting, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageServicesMainMailSetting == false)
        //            {
        //                linkMailSetting.Visible = false;
        //            }
        //        }
        //        var Is_PageForms = SecurityServices.HasPermisstionPageParrent(StringContent.Forms, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageForms == false)
        //            {
        //                linkForm.Visible = false;
        //            }
        //        }
        //        var Is_PageCustomerCare = SecurityServices.HasPermisstionPageParrent(StringContent.CustomerCare, int.Parse(Session["USer_Detail_Id"].ToString()), true);
        //        {
        //            if (Is_PageCustomerCare == false)
        //            {
        //                linkCustomerCare.Visible = false;
        //            }
        //        }
        //        #endregion
        //    }
        //}
        //#endregion

        #region[CheckLicense]
        private void GetLicense()
        {
            var ClsLicense = new bll_sad_License();
            var dtGetLicense = ClsLicense.GetLicense();
            if (dtGetLicense != null && dtGetLicense.Rows.Count > 0)
            {
                lbCustomer.Text = dtGetLicense.Rows[0]["Customer"].ToString();
                lbLicenseTye.Text = dtGetLicense.Rows[0]["LicenseType"].ToString();
                lbLicense.Text = dtGetLicense.Rows[0]["License"].ToString();
                lbKey.Text = dtGetLicense.Rows[0]["Key"].ToString();
                lbCreateDateLicense.Text = String.Format("{0:dd/MM/yyyy}", dtGetLicense.Rows[0]["DateActived"]);
                lbVersion.Text = dtGetLicense.Rows[0]["Version"].ToString();
                lbStatustActive.Text = dtGetLicense.Rows[0]["StatusActived"].ToString();
            }
        }
        #endregion

        #region[File-Manager]
        //Button
        protected void btnCreateFolder_Click(object sender, DirectEventArgs e)
        {
            txtPath.Text = pathFolderRoot;
            txtFolderName.Text = string.Empty;
            hdPath.Text = string.Empty;
            WindowCreateFolder.Show();
        }
        protected void btnDelFolder_Click(object sender, DirectEventArgs e)
        {
            var sm = grFolder.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        if (FileExists(row.RecordID))
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi",
                                "Tập tin còn tồn tại trong thư mục.Vui lòng xoá tập tin trước !");
                        }
                        else
                        {
                            var pathUrl = Server.MapPath(pathFolderRoot + row.RecordID);
                            Directory.Delete(pathUrl);
                            HTML.clsFormat.Alert_INFO("Thông Báo", "Xoá thư mục thành công !");
                        }
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            GetFolderRoot();
        }
        protected void btnUpload_Click(object sender, DirectEventArgs e)
        {
            if (String.IsNullOrEmpty(FileUploadPanel.FileName))
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", "Không có tập tin được chọn !");
            }
            else if (String.IsNullOrEmpty(hdPath.Text))
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", "Vui lòng chọn thư mục cần tải lên !");
            }
            else
            {
                if (FileNameExists(hdPath.Text + "/"))
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi",
                        "Tên tập tin này đã tồn tại trong thư mục " + hdPath.Text +
                        " vui lòng đổi tên tập tin trước khi tải lên !");
                }
                else
                {
                    Upload(Server.MapPath(pathFolderRoot + hdPath.Text + "/"));
                    GetFile(hdPath.Text);
                }
            }
        }
        protected void btnRefreshFolder_Click(object sender, DirectEventArgs e)
        {
            var sm = grFolder.GetSelectionModel() as CheckboxSelectionModel;
            hdPath.Text = string.Empty;
            GetFolderRoot();
            GetFile(string.Empty);
            if (sm != null) sm.ClearSelection();
        }
        protected void btnCreate_Click(object sender, DirectEventArgs e)
        {
            if (String.IsNullOrEmpty(txtFolderName.Text) && String.IsNullOrEmpty(txtPath.Text))
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", "Vui lòng nhập vào tên thư mục để tạo mới");
            }
            else
            {
                try
                {
                    var pathUrl = Server.MapPath(pathFolderRoot + txtFolderName.Text);
                    txtPath.Text = pathUrl;
                    Directory.CreateDirectory(pathUrl);
                    HTML.clsFormat.Alert_INFO("Thông Báo",
                        "Thư mục " + txtFolderName.Text + " được khởi tạo thành công !");
                    WindowCreateFolder.Close();
                    GetFolderRoot();
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
                }

            }
        }
        protected void btnCloseFolder_Click(object sender, DirectEventArgs e)
        {
            WindowCreateFolder.Close();
        }
        protected void btnSubmitSelect_Click(object sender, DirectEventArgs e)
        {
            foreach (var row in ImageView.SelectedRows)
            {
                lbFileName.Text = row.RecordID;
            }
        }
        protected void btnDelFile_Click(object sender, DirectEventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(hdPath.Text) && String.IsNullOrEmpty(lbFileName.Text))
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Vui lòng chọn tập tin để xoá !");
                }
                else
                {
                    foreach (var row in ImageView.SelectedRows)
                    {
                        File.Delete(HttpContext.Current.Server.MapPath(pathFolderRoot + hdPath.Text + "/") +
                                    row.RecordID + "");
                    }
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Xoá tập tin thành công !");
                    GetFile(hdPath.Text);
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        protected void btnClear_Click(object sender, DirectEventArgs e)
        {
            ImageView.DeselectAll();
            lbFileName.Text = string.Empty;
        }
        protected void btnViewFile_Click(object sender, DirectEventArgs e)
        {
            if (lbFileName.Text.EndsWith(".xls") || lbFileName.Text.EndsWith(".xlsx"))
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + "../../Img/Excel-icon.png";
                ImageFile.ImageUrl = strURL;
                WindowViewFile.Show();
            }
            else if (lbFileName.Text.EndsWith(".doc") || lbFileName.Text.EndsWith(".docx"))
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + "../../Img/Word-icon.png";
                ImageFile.ImageUrl = strURL;
                WindowViewFile.Show();
            }
            else if (lbFileName.Text.EndsWith(".pdf"))
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + "../../Img/PDF-icon.png";
                ImageFile.ImageUrl = strURL;
                WindowViewFile.Show();
            }
            else if (lbFileName.Text.EndsWith(".xml"))
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + "../../Img/XML-icon.png";
                ImageFile.ImageUrl = strURL;
                WindowViewFile.Show();
            }
            else if (lbFileName.Text.EndsWith(".txt"))
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + "../../Img/Text-icon.png";
                ImageFile.ImageUrl = strURL;
                WindowViewFile.Show();
            }
            else
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + lbFileName.Text;
                ImageFile.ImageUrl = strURL;
                WindowViewFile.Show();
            }
        }
        protected void btnDownload_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var strURL = pathFolderRoot + hdPath.Text + "/" + lbFileName.Text;
                var req = new WebClient();
                var response = HttpContext.Current.Response;
                response.Clear();
                response.ClearContent();
                response.ClearHeaders();
                response.Buffer = true;
                response.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(strURL) + "\"");
                byte[] data = req.DownloadData(Server.MapPath(strURL));
                response.BinaryWrite(data);
                response.End();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        protected void btnCloseFileManager_Click(object sender, DirectEventArgs e)
        {
            WindowFileManager.Close();
        }
        protected void linkFileManager_Click(object sender, DirectEventArgs e)
        {
            WindowFileManager.Show();
        }
        //Store
        protected void StoreFolder_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetFolderRoot();
        }
        protected void StoreFile_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetFile(hdPath.Text);
        }
        //Double-Click
        protected void PathDb_Click(object sender, DirectEventArgs e)
        {
            var Name = e.ExtraParams["Name"];
            GetFile(Name);
            hdPath.Text = Name;
        }
        //Row-Click
        protected void Select_Rows(object sender, DirectEventArgs e)
        {
            foreach (var row in ImageView.SelectedRows)
            {
                lbFileName.Text = row.RecordID;
            }
        }
        #endregion

        #region[Method-FileManager]
        private void GetFolderRoot()
        {
            var pathRoot = Server.MapPath(pathFolderRoot);
            var dir = new DirectoryInfo(pathRoot);
            StoreFolder.DataSource = dir.GetDirectories("*.*");
            StoreFolder.DataBind();
        }
        private void GetFile(string File)
        {
            var pathUrl = Server.MapPath(pathFolderRoot + File + "/");
            var files = Directory.GetFiles(pathUrl);
            var data = new List<object>(files.Length);
            foreach (var fileName in files)
            {
                var fi = new FileInfo(fileName);
                if (fi.Name.EndsWith(".xls") || fi.Name.EndsWith(".xlsx"))
                {
                    data.Add(new
                    {
                        name = fi.Name,
                        url = pathFolderRoot + File + "/" + "../../Img/Excel-icon.png"
                    });
                }
                else if (fi.Name.EndsWith(".doc") || fi.Name.EndsWith(".docx"))
                {
                    data.Add(new
                    {
                        name = fi.Name,
                        url = pathFolderRoot + File + "/" + "../../Img/Word-icon.png"
                    });
                }
                else if (fi.Name.EndsWith(".pdf"))
                {
                    data.Add(new
                    {
                        name = fi.Name,
                        url = pathFolderRoot + File + "/" + "../../Img/PDF-icon.png"
                    });
                }
                else if (fi.Name.EndsWith(".xml"))
                {
                    data.Add(new
                    {
                        name = fi.Name,
                        url = pathFolderRoot + File + "/" + "../../Img/XML-icon.png"
                    });
                }
                else if (fi.Name.EndsWith(".txt"))
                {
                    data.Add(new
                    {
                        name = fi.Name,
                        url = pathFolderRoot + File + "/" + "../../Img/Text-icon.png"
                    });
                }
                else
                {
                    data.Add(new
                    {
                        name = fi.Name,
                        url = pathFolderRoot + File + "/" + fi.Name
                    });
                }
            }
            StoreFile.DataSource = data;
            StoreFile.DataBind();
        }
        private bool FileExists(string File)
        {
            var check = false;
            var pathUrl = Server.MapPath(pathFolderRoot + File + "/");
            var files = Directory.GetFiles(pathUrl);
            foreach (var fileName in files)
            {
                var fi = new FileInfo(fileName);
                if (fi.Exists)
                {
                    check = true;
                }
            }
            return check;
        }
        private bool FileNameExists(string File)
        {
            var check = false;
            var pathUrl = Server.MapPath(pathFolderRoot + File + "/");
            var files = Directory.GetFiles(pathUrl);
            foreach (var fileName in files)
            {
                var fi = new FileInfo(fileName);
                if (fi.Name == FileUploadPanel.FileName)
                {
                    check = true;
                }
            }
            return check;
        }
        private void Upload(string path)
        {
            try
            {
                string tpl;
                tpl = "Tập tin đã tải lên : {0} <br/>Dung lượng : {1} bytes";
                if (FileUploadPanel.HasFile)
                {
                    if (CheckFileType())
                    {
                        FileUploadPanel.PostedFile.SaveAs(path + FileUploadPanel.FileName);
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.INFO,
                            Title = "Thông báo",
                            Message =
                                string.Format(tpl, FileUploadPanel.PostedFile.FileName,
                                    FileUploadPanel.PostedFile.ContentLength)
                        });
                    }
                    else
                    {
                        X.Msg.Show(new MessageBoxConfig
                        {
                            Buttons = MessageBox.Button.OK,
                            Icon = MessageBox.Icon.ERROR,
                            Title = "Lỗi",
                            Message = "Tập tin " + FileUploadPanel.FileName.Split('.')[1] + " không được hỗ trợ !"
                        });
                    }
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.ERROR,
                        Title = "Lỗi",
                        Message = "Không có tập tin được chọn"
                    });
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private bool CheckFileType()
        {
            var check = FileUploadPanel.PostedFile.FileName.EndsWith(".zip") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".jpg") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".png") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".gif") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".txt") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".xls") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".xlsx") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".doc") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".docx") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".pdf") ||
                         FileUploadPanel.PostedFile.FileName.EndsWith(".xml");
            return check;
        }
        #endregion

        #region[Get-Ip-Local]
        /*
         * GetLocalIPv4(NetworkInterfaceType.Ethernet);Lấy địa chỉ IPv4 Cáp mạng
         * GetLocalIPv4(NetworkInterfaceType.Wireless80211);Lấy địa chỉ IPv4 Wifi
         */
        public string GetLocalIPv4(NetworkInterfaceType _type)
        {
            string output = "";
            foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up)
                {
                    foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            output = ip.Address.ToString();
                        }
                    }
                }
            }
            return output;
        }
        #endregion
    }
}